//
//  main.m
//  Vcf
//
//  Created by Mac OS on 16-5-9.
//  Copyright (c) 2016年 lms. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Tel.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        // insert code here...
        //NSLog(@"Hello, World!");
        
        NSString *path = @"/Users/mac/Desktop/苹果1512廖梦诗/5/2/2016-04-27 08-59-55.vcf";
    
        NSError *err;
        NSString *vcf = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
        if(err!=nil){
        
            NSLog(@"%@",err);
        }
        
//        NSLog(@"%@",vcf);
        NSArray *arr = [vcf componentsSeparatedByString:@"\n"];
        
        
        NSMutableArray *res = [NSMutableArray new];
        NSMutableDictionary *dict;
        for(NSString *line in arr){
        
            if([line hasPrefix:@"BEGIN"]){
                NSLog(@"开始一条新的记录");
                dict = [NSMutableDictionary new];
            
            }
            
            if([line hasPrefix:@"END"]){
                NSLog(@"这条记录结束了");
                [res addObject:dict];
            
            }
            
            
            if([line hasPrefix:@"TEL"]){
               // NSLog(@"找到一条电话记录! %@",line);
                NSArray *tels = [line componentsSeparatedByString:@";"];
                NSString *last = tels.lastObject;
                last = [last substringFromIndex:5];
                NSLog(@"tel:%@",last);
                dict[@"tel"] = last;//简写
            //[dict setObject:last forKey:@"tel"]; 全写
            
            }
            
            if ([line hasPrefix:@"FN"]) {
                NSLog(@"找到一个FN  %@",line);
                NSArray *names = [line componentsSeparatedByString:@":"];
                NSString *name = names.lastObject;
                name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
                //NSLog(@"%@",name);
                name = name.stringByRemovingPercentEncoding;
                NSLog(@"name:%@",name);
                dict[@"name"] = name;
                
                
                                
            }

      }
        
         NSLog(@"%@",res);
        //将读取的电话薄生成一个新的文档
        [res writeToFile:@"/Users/mac/Desktop/电话薄.txt" atomically:NO];
        //生成JSONS文档
        NSData *data = [NSJSONSerialization dataWithJSONObject:res options:NSJSONWritingPrettyPrinted error:nil];
        [data writeToFile:@ "/Users/mac/Desktop/电话薄.txt" atomically:NO];
       // NSLog(@"%@",data); 用NSLog打印出来
        
        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
        
        NSMutableArray *res2 = [NSMutableArray new];
        for(NSDictionary *dict in res){
            Tel *t = [Tel new];
            [t setValuesForKeysWithDictionary:dict];
            
            [t setValue:@"新名字" forKey:@"name"];
            NSLog(@"new name:%@",t.name);
                       [res2 addObject:t];
            
            
        }
       // NSLog(@"%@",res2);
    
    }
    return 0;
}

